﻿
namespace Patterns4Net.Core.CecilExtensions
{
    using System.IO;
    using System.Reflection;
    using Mono.Cecil;
    using Patterns4Net.Attributes.DesignPatterns;

    [Singleton]
    public class AssemblyResolver : DefaultAssemblyResolver
    {
        private static readonly AssemblyResolver instance = new AssemblyResolver();

        public static AssemblyResolver Instance
        {
            get { return instance; }
        }

        public AssemblyResolver()
        {
            this.AddSearchDirectory(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
        }

        public AssemblyDefinition ReadAssembly(string filename)
        {
            this.AddSearchDirectory(Path.GetDirectoryName(filename));
            return AssemblyDefinition.ReadAssembly(filename, new ReaderParameters { AssemblyResolver = this });
        }
    }
}
